// pages/shoplist/componts/swiper.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    item: Object,
    itemId: String,
    openedId: String, // 外部传入：当前已展开的 ID
  },

  /**
   * 组件的初始数据
   */
  data: {
    startX: 0,
    offset: 0
  },

  observers: {
    'openedId': function (newVal) {
      // 若自己不是当前打开项，自动关闭
      if (newVal !== this.data.itemId && this.data.offset !== 0) {
        this.setData({ offset: 0 });
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX;
    },

    handleTouchMove(e) {
      const moveX = e.touches[0].clientX;
      const diff = moveX - this.startX;
      this.setData({
        offset: Math.min(0, Math.max(diff, -100)) // 控制最大左滑距离
      });
    },

    handleTouchEnd(e) {
      if (this.data.offset < -30) {
        this.setData({ offset: -100 });
        this.triggerEvent('collapseOthers', { id: this.data.itemId }); // 告知页面当前打开
      } else {
        this.setData({ offset: 0 });
      }
    },

    onTestTap() {
      const item = this.data.item;
      this.triggerEvent('edit', { item });
    },

    onDeleteTap() {
      this.triggerEvent('delete', { id: this.data.itemId });
    }
  }
})